package com.bigkoo.svprogresshud.view;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.logging.Logger;


public class SVCircleProgressBar extends Component implements Component.DrawTask{

    private static final String TAG = "CustomTitleBar";
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.DEBUG, 0, "TAG");

    private Paint paint;
//    private int roundColor;
    private Color roundColor;
//    private int roundProgressColor;
    private Color roundProgressColor;
    private float roundWidth;
    private int max;
    private int progress;
    private int style;
    private String sty;

    public static final int STROKE = 0;
    public static final int FILL = 1;



    public SVCircleProgressBar(Context context) {
        this(context,null);
    }

    public SVCircleProgressBar(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }
    public SVCircleProgressBar(Context context, AttrSet attrSet, int resId) {
        super(context, attrSet, resId);
    }
    public SVCircleProgressBar(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        paint = new Paint();

        if (attrSet.getAttr("svprogress_roundColor").isPresent()) {
            roundColor = attrSet.getAttr("svprogress_roundColor").get().getColorValue();
        } else {
            Logger.getLogger(TAG).severe("hong-> svprogress_roundColor is not present");
        }

        if (attrSet.getAttr("svprogress_roundProgressColor").isPresent()) {
            roundProgressColor = attrSet.getAttr("svprogress_roundProgressColor").get().getColorValue();
        } else {
            HiLog.error(LABEL,"hong-> svprogress_roundProgressColor is not present");
        }

        if (attrSet.getAttr("svprogress_roundWidth").isPresent()) {
            roundWidth = attrSet.getAttr("svprogress_roundWidth").get().getDimensionValue();
        } else {
            HiLog.error(LABEL,"hong-> svprogress_roundWidth is not present");
        }

        if (attrSet.getAttr("svprogress_max").isPresent()) {
            max = attrSet.getAttr("svprogress_max").get().getIntegerValue();
        } else {
            HiLog.error(LABEL,"hong-> svprogress_max is not present");
        }

        if (attrSet.getAttr("svprogress_style").isPresent()) {
            sty = attrSet.getAttr("svprogress_style").get().getStringValue();
        } else {
            HiLog.error(LABEL,"hong-> svprogress_style is not present");
        }

        addDrawTask(this::onDraw);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        int centre = getWidth() / 2; // 获取圆心的x坐标
        int radius = (int) (centre - roundWidth / 2); // 圆环的半径
        paint.setAntiAlias(true);
        paint.setColor(roundColor);
        paint.setStyle(Paint.Style.STROKE_STYLE);
        paint.setStrokeWidth(roundWidth);
        canvas.drawCircle(centre, centre, radius, paint);




        paint.setStrokeWidth(5);
        paint.setColor(Color.BLACK);
        RectFloat oval = new RectFloat(centre - radius, centre - radius, centre + radius, centre + radius);
        Arc arc = new Arc(270, 360 * progress / max, false);
        switch (sty) {
            case "STROKE": {
                paint.setStyle(Paint.Style.STROKE_STYLE);
                canvas.drawArc(oval, arc, paint); // 根据进度画圆弧
                break;
            }
            case "FILL": {
                paint.setStyle(Paint.Style.FILLANDSTROKE_STYLE);
                if (progress != 0)
                    canvas.drawArc(oval, arc, paint); // 根据进度画圆弧
                break;
            }
        }
    }


    public synchronized int getMax() {
        return max;
    }

    /**
     * 设置进度的最大值
     *
     * @param max
     */
    public synchronized void setMax(int max) {
        if (max < 0) {
            throw new IllegalArgumentException("max not less than 0");
        }
        this.max = max;
    }

    /**
     * 获取进度.需要同步
     *
     * @return
     */
    public synchronized int getProgress() {
        return progress;
    }

    /**
     * 设置进度，此为线程安全控件，由于考虑多线的问题，需要同步
     * 刷新界面调用postInvalidate()能在非UI线程刷新
     *
     * @param progress
     */
    public synchronized void setProgress(int progress) {
        if (progress < 0) {
            throw new IllegalArgumentException("progress not less than 0");
        }
        if (progress > max) {
            progress = max;
        }
        if (progress <= max) {
            this.progress = progress;
            invalidate();
        }

    }

    public Color getCircleColor() {
        return roundColor;
    }

    public void setCircleColor(Color circleColor) {
        this.roundColor = circleColor;
    }

    public Color getCircleProgressColor() {
        return roundProgressColor;
    }

    public void setCircleProgressColor(Color circleProgressColor) {
        this.roundProgressColor = circleProgressColor;
    }

    public float getRoundWidth() {
        return roundWidth;
    }

    public void setRoundWidth(float roundWidth) {
        this.roundWidth = roundWidth;
    }


}
